import Conf from 'conf';
import { Config } from '../types';

class ConfigService {
  private conf: any;

  constructor() {
    this.conf = new Conf({
      projectName: 'cmodel-cli',
      defaults: {
        apiUrl: 'https://test-teacher.yuekedashi.com',
        currentProject: null,
        currentEnvironment: 'dev'
      }
    });
  }

  get(key: keyof Config): any {
    return this.conf.get(key);
  }

  set(key: keyof Config, value: any): void {
    this.conf.set(key, value);
  }

  getAll(): Config {
    return {
      apiUrl: this.get('apiUrl') || 'https://test-teacher.yuekedashi.com',
      token: this.get('token'),
      currentProject: this.get('currentProject'),
      currentEnvironment: this.get('currentEnvironment') || 'dev'
    };
  }

  clear(): void {
    this.conf.clear();
  }

  delete(key: keyof Config): void {
    this.conf.delete(key);
  }

  getToken(): string | null {
    return this.get('token') || null;
  }

  setToken(token: string): void {
    this.set('token', token);
  }

  removeToken(): void {
    this.delete('token');
  }

  getCurrentProject(): string | null {
    return this.get('currentProject') || null;
  }

  setCurrentProject(project: string): void {
    this.set('currentProject', project);
  }

  getCurrentEnvironment(): string {
    return this.get('currentEnvironment') || 'dev';
  }

  setCurrentEnvironment(env: string): void {
    this.set('currentEnvironment', env);
  }

  getApiUrl(): string {
    return this.get('apiUrl') || 'https://test-teacher.yuekedashi.com';
  }
}

export const config = new ConfigService();